<template>
    <div class="header">
        <!--顶部导航条-->
        <div class="nav-topbar">
            <div class="container">
                <div class="topbar-menu">
                    <a href="javascript:;">小米商城</a>
                    <a href="javascript:;">MUI</a>
                    <a href="javascript:;">云服务</a>
                    <a href="javascript:;">协议规则</a>
                </div>
                <div class="topbar-user">
                    <a href="javascript:;" v-if="username">{{username}}</a>
                    <a href="javascript:;" v-if="!username" @click="goToLogin">登录</a>
                    <a href="javascript:;">注册</a>
                    <a href="javascript:;" class="my-cart" @click="goToCart"><span class="icon-cart"></span>购物车</a>
                </div>
            </div>
        </div>
        <!--头部-->
        <div class="nav-header">
            <div class="container">
                <div class="header-logo">
                    <a href="/#/index"></a>
                </div>
                <div class="header-menu">
                    <div class="item-menu"><span>小米手机</span>
                        <div class="children">
                            <ul>
                                <li v-for="(item,index) in phoneLists" :key="index" class="product">
                                    <a href="'/#/product'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.mainImage" :alt="item.subtitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.price|currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu"><span>红米手机</span>
                        <div class="children">
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img v-lazy="require('@/public/imgs/nav-img/nav-6.png')" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img v-lazy="require('@/public/imgs/nav-img/nav-6.png')" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-6.png" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-6.png" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-6.png" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-6.png" alt="">
                                    </div>
                                    <div class="pro-name">红色手机</div>
                                    <div class="pro-price">799元</div>
                                </a>
                            </li>
                        </div>
                    </div>
                    <div class="item-menu"><span>电视</span>
                        <div class="children">
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                            <li class="product">
                                <a href="" target="_blank">
                                    <div class="pro-img">
                                        <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="">
                                    </div>
                                    <div class="pro-name">小米电视</div>
                                    <div class="pro-price">6799元</div>
                                </a>
                            </li>
                        </div>
                    </div>
                </div>
                <div class="header-search">
                    <div class="wrapper">
                        <input type="text" name="keyword">
                        <a href="javascript:;"></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "NavHeader",
        data() {
            return {
                username: this.$store.state.username,
                phoneLists: []
            }
        },
        filters: {
            //金额的过滤器
            currency(val) {
                if (!val) return '0.00';
                return '￥' + val.toFixed(2) + "元";
            }
        },
        mounted() {
            this.getProductList();
        },
        methods: {
            //1.去购物车页面
            goToCart() {
                //登录路由跳转
                this.$router.push('/cart');
            },
            //2.去登录页面
            goToLogin() {
                this.$router.push('/login');
            },
            //1.获取手机数据
            getProductList() {
                this.$get("/api/products", {
                    categoryId: 100012,
                    pageSize: 6
                }).then((res) => {
                    if (res.list.length >= 6) {
                        this.phoneLists = res.list.slice(0, 6);
                        console.log(this.phoneLists);
                    }
                });
            }
        }
    }
</script>

<style lang='scss' scoped>
    @import "~@/assets/scss/base.scss";
    @import "~@/assets/scss/config.scss";

    .header {
        .nav-topbar {
            height: 39px;
            line-height: 39px;
            background-color: #333333;
            color: #b0b0b0;

            .container {
                display: flex;
                //水平居中
                justify-content: space-between;
                //垂直居中
                align-items: center;

                a {
                    display: inline-block;
                    color: #b0b0b0;
                    margin-right: 17px;
                }

                .my-cart {
                    width: 110px;
                    background-color: #FF6600;
                    text-align: center;
                    color: #ffffff;

                    .icon-cart {
                        display: inline-block;
                        width: 16px;
                        height: 12px;
                        background: url("../../public/imgs/icon-cart-checked.png") no-repeat center;
                        background-size: contain;
                        margin-right: 4px;
                    }
                }
            }
        }

        .nav-header {
            .container {
                height: 112px;
                position: relative;

                .header-logo {
                    display: inline-block;
                    width: 55px;
                    height: 55px;
                    background-color: #ff6600;

                    a {
                        display: inline-block;
                        width: 110px;
                        height: 55px;

                        &:before {
                            /*content:用来占位*/
                            content: '';
                            display: inline-block;
                            width: 55px;
                            height: 55px;
                            background: url("../../public/imgs/mi-logo.png") no-repeat center;
                            background-size: 55px;
                            transition: all 0.5s
                        }

                        &:after {
                            content: '';
                            display: inline-block;
                            width: 55px;
                            height: 55px;
                            background: url("../../public/imgs/mi-home.png") no-repeat center;
                            background-size: 55px;
                        }

                        &:hover:before {
                            margin-left: -55px;
                            transition: margin 0.5s
                        }
                    }
                }

                .header-menu {
                    display: inline-block;
                    padding-left: 209px;
                    width: 643px;

                    .item-menu {
                        display: inline-block;
                        color: #333333;
                        font-weight: bold;
                        font-size: 16px;
                        line-height: 112px;
                        margin-right: 20px;

                        span {
                            cursor: pointer;
                        }

                        &:hover {
                            color: $colorA;

                            .children {
                                height: 220px;
                                opacity: 1;
                            }
                        }

                        .children {
                            position: absolute;
                            left: 0;
                            top: 112px;
                            width: 1226px;
                            border-top: 1px solid #e5e5e5;
                            box-shadow: 0 7px 6px 0 rgba(0, 0, 0, 0.1);
                            z-index: 10;
                            background-color: #fff;
                            height: 0;
                            opacity: 0;
                            overflow: hidden;
                            transition: height 0.5s;

                            .product {
                                float: left;
                                width: 16.6%;
                                height: 220px;
                                font-size: 12px;
                                line-height: 12px;
                                text-align: center;
                                position: relative;

                                &:before {
                                    content: '';
                                    position: absolute;
                                    top: 28px;
                                    right: 0;
                                    border-left: 1px solid #d7d7d7;
                                    height: 100px;
                                    width: 1px;
                                }

                                &:last-child:before {
                                    display: none;
                                }

                                a {
                                    display: inline-block;
                                }

                                .pro-img {
                                    height: 137px;
                                }

                                .pro-name {
                                    font-weight: bolder;
                                    margin-top: 19px;
                                    margin-bottom: 8px;
                                    color: $colorB;
                                }

                                .pro-price {
                                    color: $colorA;
                                }

                                img {
                                    height: 111px;
                                    width: auto;
                                    margin-top: 26px;
                                }
                            }
                        }
                    }
                }

                .header-search {
                    width: 309px;
                    display: inline-block;

                    .wrapper {
                        height: 50px;
                        border: 1px solid #E0e0e0;
                        display: flex;
                        align-items: center;

                        input {
                            border: none;
                            border-right: 1px solid #E0e0e0;
                            width: 264px;
                            height: 50px;
                            padding-left: 14px;
                            box-sizing: border-box;
                        }

                        a {
                            display: inline-block;
                            width: 18px;
                            height: 18px;
                            background: url("../../public/imgs/icon-search.png") no-repeat center;
                            background-size: contain;
                            margin-left: 17px;
                        }
                    }
                }
            }
        }

    }
</style>
